package com.sx.wechat.command.api;

import com.sx.core.mybatis.SessionTemplate;
import com.sx.core.vo.JsonReaderResponse;
import com.sx.wechat.command.cmd.AbstractCommand;
import com.sx.wechat.security.AuthenticationHolder;
import com.sx.wechat.security.Principal;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 挂失解挂
 * Created by lihao on 2018/3/21.
 */
@Component("list_student_order_his")
public class List_student_order_his extends AbstractCommand {
    @Autowired
    SessionTemplate sessionTemplate;
    @Autowired
    JdbcTemplate jdbcTemplate;
    @Autowired
    NamedParameterJdbcTemplate namedParameterJdbcTemplate;


    @Override
    public Object executeApi(HttpServletRequest request, HttpServletResponse response) {
        int page=obtainPage(request);
        int limit=obtainLimit(request);
        String stauts=obtain(request,"status");
        Map params=new HashMap();
        Principal principal= AuthenticationHolder.getCurrent();
        String sql="select o.*,rs.status,rs.lastupdatedate from t_order_rec_stu rs JOIN t_order o on o.id=rs.orderid WHERE rs.stuid=:stuid ";
        if(StringUtils.isNotBlank(stauts)){
            sql+=" AND rs.status=:status";
        }

        params.put("status",stauts);
        params.put("stuid",principal.getId());


        sql+=" ORDER BY o.startdate ASC ";
        List<Map<String,Object>> list=namedParameterJdbcTemplate.queryForList(sql,params);
        return new JsonReaderResponse(list);
    }
}
